from flask_script import Manager,Command

from OAPro import create_app
from OAPro.models import db,Account
from OAPro.Blueprint.views import setPassword

app = create_app()

#添加jwt登录校验
from flask_jwt import JWT

def authenticate(username, password):
    user = Account.query.filter(Account.username == username).first() #查询用户是否存在
    if user and setPassword(password) == user.password: #校验密码
        return user #登录成功

def identity(payload):
    user_id = payload['identity'] #获取token当中传递的数据
    user = Account.query.get(user_id) #校验token的合法性
    if user:
        return user
    return None
jwt = JWT(app, authenticate, identity)

#配置跨域
@app.after_request
def cors(response):
    response.headers["Access-Control-Allow-Origin"] = "*"
    response.headers["Access-Control-Allow-Methods"] = "GET,POST,PUT,DELETE"
    response.headers["Access-Control-Allow-Headers"] = "x-requested-with,content-type"
    return response


if __name__ == '__main__':
    db.create_all(app=app)
    app.run(debug=True)